/**     
 * @Title: WebSecurityConfig.java   
 * @Package me.springboot.security.config   
 * @Description: TODO
 * @author weiwei 
 * @date 2017年8月21日 上午9:54:14   
 * @version V1.0     
 */
package me.springboot.security.config;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import me.springboot.security.access.intercept.MyFilterSecurityInterceptor;
import me.springboot.security.userdetails.MyCustomUserService;
import me.springboot.security.utils.MD5Util;

/**
 * @ClassName: WebSecurityConfig
 * @Description: TODO
 * @author weiwei
 * @date 2017年8月21日 上午9:54:14
 * 
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

	protected final static Logger LOGGER = LoggerFactory.getLogger(WebSecurityConfig.class);

	@Resource
	private MyFilterSecurityInterceptor myFilterSecurityInterceptor;

	@Resource
	private SessionRegistry sessionRegistry;

	@Resource
	public MyCustomUserService customUserService;

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(customUserService).passwordEncoder(new PasswordEncoder() {
			@Override
			public String encode(CharSequence rawPassword) {
				return MD5Util.encode((String) rawPassword);
			}

			@Override
			public boolean matches(CharSequence rawPassword, String encodedPassword) {
				return encodedPassword.equals(MD5Util.encode((String) rawPassword));
			}
		}); // user Details Service验证
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().authorizeRequests().antMatchers("/login").permitAll().antMatchers("/logout").permitAll()
				.antMatchers("/images/**").permitAll().antMatchers("/js/**").permitAll().antMatchers("/css/**")
				.permitAll().antMatchers("/fonts/**").permitAll().antMatchers("/favicon.ico").permitAll()
				.antMatchers("/").permitAll().anyRequest().authenticated().and().sessionManagement().maximumSessions(1)
				.sessionRegistry(sessionRegistry).and().and().logout().invalidateHttpSession(true)
				.clearAuthentication(true).and().httpBasic();
		http.addFilterBefore(myFilterSecurityInterceptor, FilterSecurityInterceptor.class).csrf().disable();
	}

	@Bean
	public SessionRegistry getSessionRegistry() {
		SessionRegistry sessionRegistry = new SessionRegistryImpl();
		return sessionRegistry;
	}
}
